package middleware.mq;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.List;

/**
 *
 * 消费者
 *
 * Author: shen qi han
 * Date: 2022/4/3 下午10:54
 */
public class ConsumerMain {

    public static void main(String[] args) throws Exception {
        // init consumer
        DefaultMQPushConsumer consumerGroup = new DefaultMQPushConsumer("consumerGroup");
        // set addr
        consumerGroup.setNamesrvAddr("localhost:9876");

        // subscribe the topic, tag
        consumerGroup.subscribe("TopicDL", "*");
        // register the callable, then deal msg from broker
        consumerGroup.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
                System.out.printf("%s Receive New Message: %s %n", Thread.currentThread().getName(), list);
                // the msgs have been consumer success
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });

        // start consumer
        consumerGroup.start();
        System.out.println("consumer start.");

    }

}
